package com.skybai.output;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Paragraph;
import com.skybai.bean.ZtBug;
import com.skybai.bean.ZtStory;
import com.skybai.bean.ZtTask;
import com.skybai.bean.ZtUser;
import com.skybai.common.PdfReport;
import com.skybai.common.QueryUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import java.util.*;

/**
 * 输出到文件
 *
 * @author sky-bai
 * @date 2021/08/12
 */
public class OutputToFile {

    private static final Logger log = LoggerFactory.getLogger(OutputToFile.class);
    private static final PdfReport pdf = new PdfReport();

    private OutputToFile(){}

    /**
     * 输出pdf
     */
//    public static void outputToPDF() {
//        System.out.println("请输入项目编号");
//        Scanner sc = new Scanner(System.in);
//        int id = sc.nextInt();
//
//
//        List<ZtBug> bugList = QueryUtil.getBug(id);
//        for (ZtBug bug : bugList) {
//            ZtUser user = QueryUtil.getUser(bug.getAssignedTo());
//            Chunk c1 = new Chunk("Bug" + bug.getId(), PdfReport.keyFont);
//            Chunk c2 = new Chunk("\n" + bug.getTitle(), PdfReport.textFont);
//            Chunk c3 = new Chunk("\n" + user.getRealname(), PdfReport.headFont);
//            Paragraph p = new Paragraph();
//            p.add(c1);
//            p.add(c2);
//            p.add(c3);
//            pdf.setDiv(p);
//        }
//
//        List<ZtTask> taskList = QueryUtil.getTask(id);
//        for (ZtTask task : taskList) {
//            ZtUser user = QueryUtil.getUser(task.getAssignedTo());
//            Chunk c1 = new Chunk("Task" + task.getId(), PdfReport.keyFont);
//            Chunk c2 = new Chunk("\n" + task.getName(), PdfReport.textFont);
//            Chunk c3 = new Chunk("\n" + user.getRealname(), PdfReport.headFont);
//            Paragraph p = new Paragraph();
//            p.add(c1);
//            p.add(c2);
//            p.add(c3);
//            pdf.setDiv(p);
//        }
//
//        List<ZtStory> storyList = QueryUtil.getStory(id);
//        for (ZtStory story : storyList) {
//            ZtUser user = QueryUtil.getUser(story.getAssignedTo());
//            Chunk c1 = new Chunk("Story" + story.getId(), PdfReport.keyFont);
//            Chunk c2 = new Chunk("\n" + story.getTitle(), PdfReport.textFont);
//            Chunk c3 = new Chunk("\n" + user.getRealname(), PdfReport.headFont);
//            Paragraph p = new Paragraph();
//            p.add(c1);
//            p.add(c2);
//            p.add(c3);
//            pdf.setDiv(p);
//        }
//    }
    public static void printUserContent(int projectId, int printOption, int userId) {
        String userAccount = QueryUtil.getUserAccount(userId);
        if (printOption == QueryUtil.STORY_PRINT) {
            List<ZtStory> list = new LinkedList<>();
            List<ZtStory> storyList = QueryUtil.getStory(projectId);
            for (ZtStory story : storyList) {
                if (userAccount.equals(story.getAssignedTo())) {
                    list.add(story);
                }
            }
            OutputToFile.printUserStory(list);
        } else if (printOption == QueryUtil.TASK_PRINT) {
            List<ZtTask> list = new LinkedList<>();
            List<ZtTask> taskList = QueryUtil.getTask(projectId);
            for (ZtTask task : taskList) {
                if (userAccount.equals(task.getAssignedTo())) {
                    list.add(task);
                }
            }
            OutputToFile.printUserTask(list);

        } else if (printOption == QueryUtil.BUG_PRINT) {
            List<ZtBug> list = new LinkedList<>();
            List<ZtBug> bugList = QueryUtil.getBug(projectId);
            for (ZtBug bug : bugList) {
                if (userAccount.equals(bug.getAssignedTo())) {
                    list.add(bug);
                }
            }
            OutputToFile.printUserBug(list);
        }
        pdf.close();
    }

    private static void printUserBug(List<ZtBug> list) {
        for (ZtBug bug : list) {
            ZtUser user = QueryUtil.getUser(bug.getAssignedTo());
            Chunk c1 = new Chunk("Bug" + bug.getId(), PdfReport.keyFont);
            Chunk c2 = new Chunk("\n" + bug.getTitle(), PdfReport.textFont);
            Chunk c3 = new Chunk("\n" + user.getRealname(), PdfReport.headFont);
            Paragraph p = new Paragraph();
            p.add(c1);
            p.add(c2);
            p.add(c3);
            pdf.setDiv(p);
        }

    }

    private static void printUserTask(List<ZtTask> list) {
        for (ZtTask task : list) {
            ZtUser user = QueryUtil.getUser(task.getAssignedTo());
            Chunk c1 = new Chunk("Task" + task.getId(), PdfReport.keyFont);
            Chunk c2 = new Chunk("\n" + task.getName(), PdfReport.textFont);
            Chunk c3 = new Chunk("\n" + user.getRealname(), PdfReport.headFont);
            Paragraph p = new Paragraph();
            p.add(c1);
            p.add(c2);
            p.add(c3);
            pdf.setDiv(p);
        }
    }

    private static void printUserStory(List<ZtStory> list) {
        for (ZtStory story : list) {
            ZtUser user = QueryUtil.getUser(story.getAssignedTo());
            Chunk c1 = new Chunk("Story" + story.getId(), PdfReport.keyFont);
            Chunk c2 = new Chunk("\n" + story.getTitle(), PdfReport.textFont);
            Chunk c3 = new Chunk("\n" + user.getRealname(), PdfReport.headFont);
            Paragraph p = new Paragraph();
            p.add(c1);
            p.add(c2);
            p.add(c3);
            pdf.setDiv(p);
        }
    }


    public static void print(int projectId, int printOption) {
        if (printOption == QueryUtil.ALL_PRINT) {
            List<ZtStory> storyList = QueryUtil.getStory(projectId);
            printUserStory(storyList);

            List<ZtTask> taskList = QueryUtil.getTask(projectId);
            printUserTask(taskList);

            List<ZtBug> bugList = QueryUtil.getBug(projectId);
            printUserBug(bugList);

        } else if (printOption == QueryUtil.STORY_PRINT) {
            List<ZtStory> storyList = QueryUtil.getStory(projectId);
            printUserStory(storyList);

        } else if (printOption == QueryUtil.TASK_PRINT) {
            List<ZtTask> taskList = QueryUtil.getTask(projectId);
            printUserTask(taskList);

        } else if (printOption == QueryUtil.BUG_PRINT) {
            List<ZtBug> bugList = QueryUtil.getBug(projectId);
            printUserBug(bugList);
        }

        pdf.close();
    }

    public static void printGroupByPerson(int projectId, int printOption) {
        if (printOption == QueryUtil.ALL_PRINT) {
            List<ZtStory> storyList = QueryUtil.getStory(projectId);
            storyList.sort(Comparator.comparing(ZtStory::getAssignedTo));
            printUserStory(storyList);

            List<ZtTask> taskList = QueryUtil.getTask(projectId);
            taskList.sort(((ztTask, t1) -> ztTask.getAssignedTo().compareTo(t1.getAssignedTo())));
            printUserTask(taskList);

            List<ZtBug> bugList = QueryUtil.getBug(projectId);
            bugList.sort(((ztBug, t1) -> ztBug.getAssignedTo().compareTo(t1.getAssignedTo())));
            printUserBug(bugList);

        } else if (printOption == QueryUtil.STORY_PRINT) {
            List<ZtStory> storyList = QueryUtil.getStory(projectId);
            storyList.sort((ztStory, t1) -> ztStory.getAssignedTo().compareTo(t1.getAssignedTo()));
            printUserStory(storyList);

        } else if (printOption == QueryUtil.TASK_PRINT) {
            List<ZtTask> taskList = QueryUtil.getTask(projectId);
            taskList.sort(((ztTask, t1) -> ztTask.getAssignedTo().compareTo(t1.getAssignedTo())));
            printUserTask(taskList);

        } else if (printOption == QueryUtil.BUG_PRINT) {
            List<ZtBug> bugList = QueryUtil.getBug(projectId);
            bugList.sort(((ztBug, t1) -> ztBug.getAssignedTo().compareTo(t1.getAssignedTo())));
            printUserBug(bugList);
        }

        pdf.close();
    }
}
